package com.yhy.dubbo.service.mongo;

import com.yhy.domain.mongo.Friend;
import com.yhy.domain.mongo.UserLike;
import com.yhy.domain.vo.PageResult;
import com.yhy.dubbo.api.mongo.UserLikeApi;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Long queryEachLoveCount(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);
        return mongoTemplate.count(new Query(criteria), Friend.class);
    }

    @Override
    public Long queryLoveCount(Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);
        return mongoTemplate.count(new Query(criteria), UserLike.class);
    }

    @Override
    public Long queryFanCount(Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId);
        return mongoTemplate.count(new Query(criteria), UserLike.class);
    }

    //获取所有Friend的id的集合List<Long>
    @Override
    public PageResult queryEachLoveList(Integer page, Integer pagesize, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);

        long count = mongoTemplate.count(new Query(criteria), Friend.class);

        Query query = new Query(criteria).skip((page-1)* pagesize).limit(pagesize);
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        List<Long> userIds = new ArrayList<>();
        for (Friend friend : friendList) {
            userIds.add(friend.getFriendId());
        }
        return new PageResult(page,pagesize, (int) count,userIds);
    }

    //获取所有UserLike表我喜欢用户的id的集合List<Long>
    @Override
    public PageResult queryLoveList(Integer page, Integer pagesize, Long userId) {
        Criteria criteria = Criteria.where("userId").is(userId);

        long count = mongoTemplate.count(new Query(criteria), UserLike.class);

        Query query = new Query(criteria).skip((page-1)* pagesize).limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        List<Long> userIds = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            userIds.add(userLike.getLikeUserId());
        }
        return new PageResult(page,pagesize, (int) count,userIds);
    }

    //获取所有UserLike表喜欢我的用户的id的集合List<Long>
    @Override
    public PageResult queryFanList(Integer page, Integer pagesize, Long userId) {
        Criteria criteria = Criteria.where("likeUserId").is(userId);

        long count = mongoTemplate.count(new Query(criteria), UserLike.class);

        Query query = new Query(criteria).skip((page-1)* pagesize).limit(pagesize);
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);
        List<Long> userIds = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            userIds.add(userLike.getUserId());
        }
        return new PageResult(page,pagesize, (int) count,userIds);
    }

    @Override
    public boolean save(Long userId, Long likeUserId) {
        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeUserId);
        userLike.setCreated(System.currentTimeMillis());
        mongoTemplate.save(userLike);
        Criteria criteria = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        return mongoTemplate.exists(new Query(criteria),UserLike.class);
    }

    @Override
    public boolean delete(Long userId, Long likeUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        mongoTemplate.remove(new Query(criteria),UserLike.class);
        Criteria criteria1 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);
        return mongoTemplate.exists(new Query(criteria1),UserLike.class);
    }
}